<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box-wrapper {
            column-count: 6;
            column-gap: 0;
            counter-reset: item-counter;
        }

        .box-item {
            position: relative;
            box-sizing: border-box;
            break-inside: avoid;
            padding: 10px;
        }

        .box-item::before {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            display: block;
            counter-increment: item-counter;
            content: counter(item-counter);
            font-size: 30px;
            color: #fff;
        }

        .box-item > div {
            height: 100%;
            background: #4286F5;
            box-sizing: border-box;
        }

        .loading,
        .loading:before,
        .loading:after {
            background: #01b909;
            animation: load1 1s infinite ease-in-out;
            width: 1em;
            height: 4em;
            border-radius: 8px;
        }

        .loading:before,
        .loading:after {
            position: absolute;
            top: 0;
            content: '';
        }

        .loading:before {
            left: -1.5em;
        }

        .loading {
            position: fixed;
            bottom: 0;
            left: 50%;
            transform: translateY(-50%);
            margin: 20px auto;
            font-size: 11px;
            animation-delay: 0.16s;
        }

        .loading:after {
            left: 1.5em;
            animation-delay: 0.32s;
        }

        @keyframes load1 {
            0%,
            80%,
            100% {
                box-shadow: 0 0 #01b909;
                height: 3em;
            }
            40% {
                /* 实现上部拉伸 */
                box-shadow: 0 -2em #01b909;
                /* 实现下部拉伸 */
                height: 4em;
            }
        }
    </style>
</head>
<body>
<div class="box-wrapper"></div>
<script>
    const INITIAL_NUMBER = 40
    const boxWrapper = document.querySelector('.box-wrapper')

    function createFragment(num) {
        const fragment = document.createDocumentFragment()
        for (let i = 0; i < num; i++) {
            const div = document.createElement('div')
            const randomHeight = Math.random() * 400 + 100
            div.innerHTML = '<div></div>'
            div.style.height = `${randomHeight}px`
            div.classList.add('box-item')
            fragment.appendChild(div)
        }
        return fragment
    }

    function createLoading() {
        let loading = document.querySelector('.loading')
        if (!loading) {
            loading = document.createElement('div')
            loading.classList.add('loading')
        }
        return loading
    }

    function init() {
        boxWrapper.appendChild(createFragment(INITIAL_NUMBER))
    }

    function scrollHandler() {
        const {scrollTop, clientHeight, offsetHeight} = document.documentElement
        const nearBottom = offsetHeight - clientHeight === scrollTop

        if (nearBottom) {
            const num = Math.ceil(Math.random() * 10) + 10
            const fragment = createFragment(num)
            const loading = createLoading()
            document.body.appendChild(loading)
            setTimeout(() => {
                document.body.removeChild(loading)
                boxWrapper.appendChild(fragment)
            }, 1000)
        }
    }

    function debounce(callback, delay) {
        let timer = null
        return function () {
            clearTimeout(timer)
            timer = setTimeout(callback, delay)
        }
    }

    window.addEventListener('load', init)
    window.addEventListener('scroll', debounce(scrollHandler, 100))

</script>
</body>
</html>
